package com.ruoyi.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.ServiceMain;
import com.ruoyi.domain.ServicePeople;
import com.ruoyi.domain.UserAddress;
import com.ruoyi.enums.DelEnums;
import com.ruoyi.enums.OrderBusinessType;
import com.ruoyi.enums.OrderStatus;
import com.ruoyi.mapper.ServiceMainMapper;
import com.ruoyi.mapper.ServicePeopleMapper;
import com.ruoyi.mapper.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.OrderMapper;
import com.ruoyi.domain.Order;
import com.ruoyi.service.IOrderService;

/**
 * 订单信息Service业务层处理
 * 
 * @author jack
 * @date 2025-01-13
 */
@Service
public class OrderServiceImpl implements IOrderService 
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private ServiceMainMapper serviceMainMapper;
    @Autowired
    private ServicePeopleMapper servicePeopleMapper;

    /**
     * 查询订单信息
     * 
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询订单信息列表
     * 
     * @param order 订单信息
     * @return 订单信息
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        List<Order> resp = orderMapper.selectOrderList(order);
        resp.forEach(item->{
            item.setAddressDetail(userAddressMapper.selectUserAddressById(item.getAddressId()));
            if(item.getBusinessType().equals(OrderBusinessType.CLEAN_DAILY.getValue())){
                ServiceMain main = serviceMainMapper.selectServiceMainById(item.getBusinessId());
                ServicePeople people = new ServicePeople();
                people.setServiceId(item.getBusinessId());
                main.setServicePeopleList(servicePeopleMapper.selectServicePeopleList(people));
                item.setBusinessDetail(main);
            }
            item.setServicePersonDetail(servicePeopleMapper.selectServicePeopleById(item.getSerivcePersonId()));
        });
        return resp;
    }

    /**
     * 新增订单信息
     * 
     * @param order 订单信息
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单信息
     * 
     * @param order 订单信息
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单信息
     * 
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除订单信息信息
     * 
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }

    // 下单
    @Override
    public AjaxResult startOrder(Order order) {
        // 进行校验
        if(order.getBusinessType() == null){
            return AjaxResult.error("请选择业务类型");
        }
        if(order.getAddressId() == null){
            return AjaxResult.error("请输入订单地址");
        }
        if(order.getBusinessId() == null){
            return AjaxResult.error("请选择业务ID");
        }
        // 通过校验进行业务处理
        order.setOrderSn(IdUtil.getSnowflakeNextIdStr());
        order.setUserId(SecurityUtils.getUserId());
        order.setOrderStatus(OrderStatus.WAIT_PAY.getDesc());
        order.setOrderMessage("订单创建成功，请及时完成支付");
        order.setDelFlag(DelEnums.STAY.getCode());
        orderMapper.insertOrder(order);
        return AjaxResult.success("创建订单完成");
    }

    // 支付订单
    @Override
    public AjaxResult payOrder(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        // 完成支付
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 检查订单状态是否正确
            if(!OrderStatus.WAIT_PAY.getDesc().equals(item.getOrderStatus())){
                return AjaxResult.error("订单状态不正确");
            }
            // 设置订单为待接单
            item.setOrderStatus(OrderStatus.WAIT_DELIVER.getDesc());
            item.setOrderMessage("已付款，等待卖家接单");
            item.setPayTime(DateUtils.getNowDate());
            orderMapper.updateOrder(item);// 更新订单
            return AjaxResult.success("订单支付成功");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    // 订单超时
    @Override
    public AjaxResult timeOutOrder(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 检查订单状态
            if(!OrderStatus.WAIT_PAY.getDesc().equals(item.getOrderStatus())){
                return AjaxResult.error("订单状态不正确");
            }
            item.setOrderStatus(OrderStatus.CANCEL.getDesc());
            item.setOrderMessage("订单超时，已取消");
            orderMapper.updateOrder(item);
            return AjaxResult.success("订单超时");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    // 接单
    @Override
    public AjaxResult acceptOrder(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        if(order.getSerivcePersonId() == null){
            return AjaxResult.error("请输入接单人ID");
        }
        // 完成接单
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 检查订单状态
            if(!OrderStatus.WAIT_DELIVER.getDesc().equals(item.getOrderStatus())){
                return AjaxResult.error("订单状态不正确");
            }
            // 设置订单为待服务
            item.setOrderStatus(OrderStatus.WAIT_SERVICE.getDesc());
            item.setSerivcePersonId(order.getSerivcePersonId());
            item.setOrderMessage("卖家已接单，等待上门服务");
            item.setReceiveTime(DateUtils.getNowDate());
            orderMapper.updateOrder(item);// 更新订单
            return AjaxResult.success("订单接单成功");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    // 开始服务
    @Override
    public AjaxResult startService(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        // 开始服务
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 检查订单状态
            if(!OrderStatus.WAIT_SERVICE.getDesc().equals(item.getOrderStatus())){
                return AjaxResult.error("订单状态不正确");
            }
            // 设置订单为待服务
            item.setOrderStatus(OrderStatus.SERVICING.getDesc());
            item.setOrderMessage("上门服务中……");
            orderMapper.updateOrder(item);// 更新订单
            return AjaxResult.success("订单开始成功");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    // 完成服务
    @Override
    public AjaxResult finishService(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        // 完成服务
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 检查订单状态
            if(!OrderStatus.SERVICING.getDesc().equals(item.getOrderStatus())){
                return AjaxResult.error("订单状态不正确");
            }
            // 设置订单为待服务
            item.setOrderStatus(OrderStatus.FINISH.getDesc());
            item.setOrderMessage("当前服务已完成");
            item.setDoneTime(DateUtils.getNowDate());
            orderMapper.updateOrder(item);// 更新订单
            return AjaxResult.success("订单完成成功");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }

    // 取消
    @Override
    public AjaxResult cancelOrder(Order order) {
        if(order.getId() == null){
            return AjaxResult.error("请输入订单ID");
        }
        // 完成服务
        Order item = orderMapper.selectOrderById(order.getId());
        if(item != null){
            // 设置订单为待服务
            item.setOrderStatus(OrderStatus.CANCEL.getDesc());
            item.setOrderMessage("订单已取消");
            orderMapper.updateOrder(item);// 更新订单
            return AjaxResult.success("订单取消成功");
        }else{
            return AjaxResult.error("订单不存在");
        }
    }
}
